package com.imagecollector.controller;


import com.imagecollector.common.R;
import com.imagecollector.common.constant.JwtClaimsConstant;
import com.imagecollector.common.properties.JwtProperties;
import com.imagecollector.common.utils.JwtUtil;
import com.imagecollector.domain.dto.UserDTO;
import com.imagecollector.domain.dto.UserEditDTO;
import com.imagecollector.domain.dto.UserSearchDTO;
import com.imagecollector.domain.entity.User;
import com.imagecollector.domain.vo.UserLoginVO;
import com.imagecollector.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wrf
 * @since 2024-07-31
 */
@Slf4j
@Api(tags = "用户相关接口")
@RestController
public class UserController {


    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/user/login")
    @ApiOperation(value = "WeChat登录接口")
    public R login(String code){
        log.info("正在进行登录操作，前端提交信息code:{}",code);
        User user= userService.login(code);

        /*生成jwt令牌*/
        Map<String, Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token= JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenId())
                .token(token)
                .build();

        return R.success().data("data",userLoginVO);
    }

    /*修改微信用户信息，仅可修改昵称和phone*/
    @PutMapping("/user/wx/saveOrUpdateUser")
    @ApiOperation("修改微信用户信息")
    public R saveOrUpdateUser(UserEditDTO userEditDTO){

        userService.saveOrUpdateUser(userEditDTO);

        return R.success();
    }



    /**
     *(管理员)新增用户（可选2客服 3管理员）
     */
    @PostMapping("/admin/addUser")
    @ApiOperation("(管理员)新增用户（可选2客服 3管理员）")
    public R addUser(@ApiParam("用户实体") UserDTO userDTO){
        return userService.saveOne(userDTO);
    }
    /**
     * 修改用户表用户状态 正常/冻结
     */
    @PostMapping("/admin/updateStatus")
    @ApiOperation("修改用户状态 0正常/1冻结")
    public R updateStatus(@ApiParam("用户编号")Integer id, @ApiParam("用户状态") Integer status){
        return userService.updateStatus(id, status);
    }
    /**
     * 修改用户密码
     */
    @PostMapping("/user/updatePassword")
    @ApiOperation("用户修改密码")
    public R updatePassword(@ApiParam("手机号")Integer phone, @ApiParam("旧密码") String oldPassword, @ApiParam("新密码") String password){
        return userService.updatePassword(phone, oldPassword, password);
    }
    /**
     * 根据条件分页查找用户
     */
    @PutMapping("/admin/pageQueryUsers")
    @ApiOperation("根据条件分页查找用户")
    public R pageQueryUsers(@ApiParam("用户查询条件传输实体")@RequestBody UserSearchDTO requestParam){
        return userService.pageQueryUsers(requestParam);
    }
}
